﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.Common;
using EShop.BLL;
using EShop.Model;

namespace EShop.Client
{
    public partial class frmMerchandiseOrderCollect : frmBase
    {
        private List<MerchandiseOrderDetail> oMerchandiseOrderDetail;

        private const string DEFAULT_MEMBER = "110918";
        public frmMerchandiseOrderCollect(List<MerchandiseOrderDetail> oMerchandiseOrderDetail,Bill bill)
        {
            InitializeComponent();
            this.oMerchandiseOrderDetail = oMerchandiseOrderDetail;
            this.bill = bill;
        }

        MemberInfo oMemberInfo = new MemberInfo();
        Bill bill = null;

        private void frmMerchandiseOrderCollect_Load(object sender, EventArgs e)
        {
            List<string> listMerchandiseName = new List<string>();
            oMemberInfo.MemberID = DEFAULT_MEMBER;

            Dictionary<string, int> sell = new Dictionary<string, int>();
            foreach (MerchandiseOrderDetail items in oMerchandiseOrderDetail)
            {
                try
                {
                    sell.Add(items.Barcode, items.BuyNumber);
                    listMerchandiseName.Add(items.MerchandiseName);
                }
                catch
                {
                    sell[items.Barcode] = sell[items.Barcode] + items.BuyNumber;
                }
            }

            int index = 0;
            string msg = string.Empty;
            foreach (int item in sell.Values)
            {
                msg += listMerchandiseName[index] + "*" + item.ToString() + "\r\n";
                index++;
            }
            this.txtMsg.Text = msg;
            this.txtTotalSale.Text = FormatManager.FormatDecimal(bill.TotalSalePrice);
            this.txtPayFact.Text = FormatManager.FormatDecimal(bill.TotalSalePrice);
            this.txtDiscount.Text = FormatManager.FormatDecimal(1);
            this.txtPayReal.Text = FormatManager.FormatDecimal(0);
            this.btnInputMemberID_Click(sender, e);
        }

        private void GetMemberID()
        {
            frmMerchandiseOrderInputMemberID oMerchandiseOrderInputMemberID = new frmMerchandiseOrderInputMemberID(oMemberInfo);
            oMerchandiseOrderInputMemberID.Owner = this;
            DialogResult dialogResult = oMerchandiseOrderInputMemberID.ShowDialog();
            if (dialogResult == DialogResult.Retry)
            {
                this.DialogResult = DialogResult.Cancel;
                return;
            }
            if (dialogResult != DialogResult.OK)
            {
                oMemberInfo.MemberID = DEFAULT_MEMBER;
                oMemberInfo.Discount = 1.00m;
            }
            this.txtDiscount.Text = Common.FormatManager.FormatDecimal(oMemberInfo.Discount);
            this.lblMsg.Text = "当前会员：" + oMemberInfo.MemberID;
            bill.MemberID = oMemberInfo.MemberID;
            this.txtPayReal.SelectAll();
            this.txtPayReal.Focus();
            this.DialogResult = DialogResult.None;
        }

        private void txtPayFact_TextChanged(object sender, EventArgs e)
        {
            decimal paycount = Convert.ToDecimal(txtPayReal.Text.Trim());
            lblZhaoling.Text = (paycount - Convert.ToDecimal(txtPayFact.Text)).ToString();
        }

        private void txtPayA_TextChanged(object sender, EventArgs e)
        {
            try
            {
                decimal paycount = Convert.ToDecimal(txtPayReal.Text.Trim());
                lblZhaoling.Text = (paycount - Convert.ToDecimal(txtPayFact.Text)).ToString();
            }
            catch
            {
                this.txtPayReal.Text = "0";
            }
        }

        private void txtDiscount_Enter(object sender, EventArgs e)
        {
            this.txtDiscount.SelectAll();
        }

        private void btnComplete_Click(object sender, EventArgs e)
        {
            lblError.Visible = false;
            if (Convert.ToDecimal(lblZhaoling.Text) < 0)
            {
                lblError.Text = "请输入客户已付金额";
                lblError.Visible = true;
                txtPayReal.SelectAll();
                txtPayReal.Focus();
                return;
            }
            else
            {
                MerchandiseOrderBLL oMerchandiseOrderBLL = new MerchandiseOrderBLL();
                foreach (MerchandiseOrderDetail item in oMerchandiseOrderDetail)
                {
                    int index = 0;
                    if (oMerchandiseOrderBLL.UpdateMerchandiseSellQty(item.Barcode, -item.BuyNumber) == false)
                    {
                        index++;
                        MessageBox.Show("商品条码为：" + item.Barcode +
                            "\r\n\t商品名称为：" + item.MerchandiseName +
                            "在更新数量为" + item.BuyNumber.ToString() + item.UnitName +
                            "时产生错误，请及时联络管理员。\r\n开始执行恢复操作。", "收银失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        RestoreStock(index);
                        return;
                    }

                }

                Model.MerchandiseOrder oMerchandiseOrder = new MerchandiseOrder();
                oMerchandiseOrder.PayAmount = Convert.ToDecimal(txtTotalSale.Text);
                oMerchandiseOrder.MemberID = oMemberInfo.MemberID;
                oMerchandiseOrder.Discount = Convert.ToDecimal(txtDiscount.Text);
                oMerchandiseOrder.RealAmount = Convert.ToDecimal(txtPayFact.Text);
                oMerchandiseOrder.EmployeeID = oShopConfigInfo.LoginID;
                oMerchandiseOrder.OrderRemark = SystemLibrary.SQLQuoteName(rtbRemark.Text);
                string strOrderID = oMerchandiseOrderBLL.InserMerchandiseOrder(oMerchandiseOrder);
                if (string.IsNullOrEmpty(strOrderID))
                {
                    MessageBox.Show("收银失败,请联络管理员");
                    return;
                }
                else
                {
                    bill.OrderID = strOrderID;
                    bill.PayFact = Convert.ToDecimal(txtPayFact.Text);
                    bill.ReturnMoney = Convert.ToDecimal(lblZhaoling.Text);
                    bill.PayReal = Convert.ToDecimal(txtPayReal.Text);
                    

                    foreach (MerchandiseOrderDetail items in oMerchandiseOrderDetail)
                    {
                        items.OrderID = Convert.ToInt32(strOrderID);
                        oMerchandiseOrderBLL.InsertMerchandiseOrderDeatil(items);
                    }
                    MessageBox.Show("完成收银");
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void btnInputMemberID_Click(object sender, EventArgs e)
        {
            GetMemberID();
        }

        private void txtDiscount_Leave(object sender, EventArgs e)
        {
            try
            {
                //txtPayFact.Text = Common.FormatManager.FormatDecimal(PayTotal * Convert.ToDecimal(txtDiscount.Text.ToString()));
            }
            catch
            {
                txtDiscount.Text = "1.00";
            }
        }
        private void RestoreStock(int index)
        {
            //foreach (MerchandiseOrderDetail item in oMerchandiseOrderDetail)
            BLL.MerchandiseOrderBLL oMerchandiseOrderBLL = new MerchandiseOrderBLL();
            for (int i = 0; i < index; i++)
            {
                if (oMerchandiseOrderBLL.UpdateMerchandiseSellQty(oMerchandiseOrderDetail[i].Barcode, oMerchandiseOrderDetail[i].BuyNumber) == false)
                {
                    MessageBox.Show("恢复操作失败，请马上停止收银，并立即联络技术人员！！！", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            //oMerchandiseOrderBLL.UpdateMerchandiseSellQty(strBarcode, iNum);
        }

        private void txtDiscount_TextChanged(object sender, EventArgs e)
        {
            try
            {
                //txtPayFact.Text = Common.FormatManager.FormatDecimal(PayTotal * Convert.ToDecimal(txtDiscount.Text.ToString()));
            }
            catch
            { }
        }
    }
}